<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Login extends CI_Controller{
	public function __construct() {
		parent::__construct ();
		$this->load->model("user_model");
		$this->load->model('category_model');
		$this->load->model('message_model');
		$whereArr = array("status"=>1);
		$this->category_list = $this->category_model->resultAll($whereArr);
	}
	/*
	 *加载登录页面
	 */
	public function index(){
		$data['category_list'] = $this->category_list;
		$this->load->view("login",$data);
	}
	/*
	 *登录功能 
	 */
	public function tologin(){
		$username = $this->input->post("username");
		$password = $this->input->post("password");
		$code = $this->input->post("code");
		$session_code = $this->session->userdata('captcha');
		$url = $this->input->post("url");
		$message = array();
		if(strtolower($code) != strtolower($session_code)){
			$this->_failure(-1, "验证码不正确!!");
		}
		if (! empty ( $username ) && ! empty ( $password )) {
			$re = $this->tologin->isExistUser ($username);
			if (! $re) {
				$this->_failure(-1, "该用户还未注册,登录失败!!");
			}else{
				$re = $this->tologin->userPwd ( $username, $password );
				if (! $re) {
					$this->_failure(-1, "密码输入错误!!");
				} else {
					
					$dataArr['uid'] = $this->change_username_to_uid($username);
					$dataArr['username'] = $username;
					$this->tologin->login ( $dataArr );
					$message['flag'] = 1;
					$message['content'] = "登录成功!!";
					$message['url'] = $url;
					$this->no_read_message_count();
					echo json_encode($message);
					exit;
					//$this->_succeed(1, "登陆成功", site_url('Main'))
				}
			}
		}
	}
	/*
	 *查找该用户的为读信息的条数 
	 */
	public function no_read_message_count(){
		$uid = $this->session->userdata('uid');
		$count = 0;
		$whereArr = "(suid='$uid' AND status=1) OR (ruid='$uid' AND status=1)";
		$message_list = $this->message_model->resultAll($whereArr,"sendtime@DESC");
		$message_list = $this->filter_message_data($message_list);
		foreach ($message_list as $key => $value){
			if($value['lookStatus']==0){
				$count++;
			}
		}
		$this->session->set_userdata('no_read_count', $count);
	}
	/*
	 *数据的筛除 
	 */
	public function filter_message_data($message_list=array()){
		$uid = $this->session->userdata('uid');
		$new_message_list = array();
		$key_arr = array();
		foreach($message_list as $key => $value){
			if(empty($new_message_list)){
				if(in_array($uid, $value)){
					$new_message_list[] = $value;
				}
				continue;
			}
			if($value['suid']!=$uid){
				$result = $value['suid'];
			}
			if($value['ruid']!=$uid){
				$result = $value['ruid'];
			}
			foreach($new_message_list as $key1 => $value1){
				if($value1['suid']!=$uid){
					$key_arr[] = $value1['suid'];
				}else{
					$key_arr[] = $value1['ruid'];
				}
			}
			if(!in_array($result, $key_arr)){
				$new_message_list[] = $value;
			}
		}
		return $new_message_list;
	}
	/*
	 *根据用户名转换为用户id 
	 */
	public function change_username_to_uid($username){
		$whereArr = array("username"=>$username,'status'=>1);
		$user = $this->user_model->row($whereArr);
		if($user){
			return $user['uid'];
		}
	}
	/*
	 *登录失败 
	 */
	private function _failure($flag,$content){
		$message = array();
		$message['flag'] = $flag;
		$message['content'] = $content;
		echo json_encode($message);
		exit;
	}
	/*
	 *登录成功 
	 */
	private function _succeed($flag,$content,$url){
		$message = array();
		$this->tologin->login ( $username );
		$message['flag'] = $flag;
		$message['content'] = $content;
		$message['url'] = $url;
		echo json_encode($message);
		exit;
	}
}